package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import model.Contact;
import model.ContactGroupManager;

/**
 * Servlet implementation class AddContact
 */
public class AddContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long cgid = Long.parseLong(request.getParameter("group"));
		System.out.println("cgid = " + Long.toString(cgid));
		String name = (String)request.getParameter("name");
		String surname = (String)request.getParameter("surname");
		String phone = (String)request.getParameter("phone");
		String email = (String)request.getParameter("email");
		System.out.println("ContactList get request : "+request.toString()+" name : "+name+" surname : "+surname+" phone : "+phone+" email : "+email);
		
		Contact newContact = new Contact(name, surname, phone, email, new ImageIcon());
		newContact.setGroup(ContactGroupManager.getInstance().getGroupFromId(cgid));
		ContactGroupManager.getInstance().AddNewContact(newContact);
		
		//request.setAttribute("contactGroupSelect", request.getParameter("group"));
		request.getRequestDispatcher("ContactList?contactGroupSelect="+cgid).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
